package com.liciecruzadinha.app;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import org.apache.http.conn.scheme.PlainSocketFactory;


public class CruzadinhaActivity extends Activity implements View.OnClickListener{
    private AcaoBotoes acaoBt = new AcaoBotoes(this);
    private int palavrasNovas = 0;
    private String extensoPalavrasString = "";
    private int[] palavrasAcertadas = new int[13];
    private FileUtil fU = new FileUtil();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cruzadinha);

//        EditText edt72_PR = (EditText)findViewById(R.id.edt72); //Periodo
//        edt72_PR.addTextChangedListener(new EventoVerificarEditText(edt72_PR, this));
//
//        EditText edt105_MG = (EditText)findViewById(R.id.edt105); //MAQUILLAGE
//        edt105_MG.addTextChangedListener(new EventoVerificarEditText(edt105_MG, this));
//
//        EditText edt57_ST = (EditText)findViewById(R.id.edt57); //SOUTIEN
//        edt57_ST.addTextChangedListener(new EventoVerificarEditText(edt57_ST, this));
//
//        EditText edt29_BG = (EditText)findViewById(R.id.edt29); //BAGAGE
//        edt72_PR.addTextChangedListener(new EventoVerificarEditText(edt29_BG, this));
//
//        EditText edt64_IO = (EditText)findViewById(R.id.edt64); //IODO
//        edt64_IO.addTextChangedListener(new EventoVerificarEditText(edt64_IO, this));
//
//        EditText edt49_NE = (EditText)findViewById(R.id.edt49); //NEON
//        edt49_NE.addTextChangedListener(new EventoVerificarEditText(edt49_NE, this));
//
//        EditText edt66_GA = (EditText)findViewById(R.id.edt64); //GASES
//        edt66_GA.addTextChangedListener(new EventoVerificarEditText(edt66_GA, this));
//
//        EditText edt89_CV = (EditText)findViewById(R.id.edt89); //CAVIAR
//        edt89_CV.addTextChangedListener(new EventoVerificarEditText(edt89_CV, this));
//
//        EditText edt88_CV = (EditText)findViewById(R.id.edt88); //CAVIAR
//        edt88_CV.addTextChangedListener(new EventoVerificarEditText(edt88_CV, this));
//
//        EditText edt99_BO = (EditText)findViewById(R.id.edt99); //SOUTIEN
//        edt99_BO.addTextChangedListener(new EventoVerificarEditText(edt99_BO, this));
//
//        EditText edt73_FL = (EditText)findViewById(R.id.edt73); //FLUOR
//        edt73_FL.addTextChangedListener(new EventoVerificarEditText(edt73_FL, this));
//
//        EditText edt102_CA = (EditText)findViewById(R.id.edt102); //CA
//        edt102_CA.addTextChangedListener(new EventoVerificarEditText(edt102_CA, this));
//
//        EditText edt107_PA = (EditText)findViewById(R.id.edt107); //PASTEUR
//        edt107_PA.addTextChangedListener(new EventoVerificarEditText(edt107_PA, this));
//
//        EditText edt39_O = (EditText)findViewById(R.id.edt39); //O
//        edt39_O.addTextChangedListener(new EventoVerificarEditText(edt39_O, this));

        ((Button) findViewById(R.id.button1)).setOnClickListener(acaoBt);
        ((Button) findViewById(R.id.button2)).setOnClickListener(acaoBt);
        ((Button) findViewById(R.id.button3)).setOnClickListener(acaoBt);
        ((Button) findViewById(R.id.button4)).setOnClickListener(acaoBt);
        ((Button) findViewById(R.id.button5)).setOnClickListener(acaoBt);
        ((Button) findViewById(R.id.button6)).setOnClickListener(acaoBt);
        ((Button) findViewById(R.id.button7)).setOnClickListener(acaoBt);
        ((Button) findViewById(R.id.button8)).setOnClickListener(acaoBt);
        ((Button) findViewById(R.id.button9)).setOnClickListener(acaoBt);
        ((Button) findViewById(R.id.button10)).setOnClickListener(acaoBt);
        ((Button) findViewById(R.id.button11)).setOnClickListener(acaoBt);
        ((Button) findViewById(R.id.button12)).setOnClickListener(acaoBt);
        ((Button) findViewById(R.id.button13)).setOnClickListener(acaoBt);

        ((Button) findViewById(R.id.button_verificar)).setOnClickListener(this);



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.cruzadinha, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void verificarTudo(){
        verificar(PalavrasConstants.PERIODO,"PERIODO",0);
        verificar(PalavrasConstants.MAQUILLAGE,"MAQUILLAGE",1);
        verificar(PalavrasConstants.SOUTIEN,"SOUTIEN",2);
        verificar(PalavrasConstants.BAGAGE,"BAGAGE",3);
        verificar(PalavrasConstants.IODO,"IODO",4);
        verificar(PalavrasConstants.NEON,"NEON",5);
        verificar(PalavrasConstants.GASES,"GASES",6);
        verificar(PalavrasConstants.CAVIAR,"CAVIAR",7);
        verificar(PalavrasConstants.BORO,"BORO",8);
        verificar(PalavrasConstants.FLUOR,"FLUOR",9);
        verificar(PalavrasConstants.CA,"CA",10);
        verificar(PalavrasConstants.PASTEUR,"PASTEUR",11);
        verificar(PalavrasConstants.O,"O",12);

        new StatusDialog(extensoPalavrasString, palavrasNovas).show(getFragmentManager(), "Mydialog");
        palavrasNovas = 0;
        extensoPalavrasString = "";
    }

    private void verificar(int[] palavraConst, String palavra, int nPalavra){
        String verificar = "";
        int fieldsClean = 0;
        EditText edtVerificar = null;
        int i = 0;

        for(i= 0; i<palavraConst.length; i++){
            edtVerificar = (EditText)findViewById(palavraConst[i]);
            verificar+=edtVerificar.getText().toString();
        }
        if (verificar.toUpperCase().equals(palavra.toUpperCase())){
            Log.d("Status", "Acertou");
            desabilitarSeqEditText(palavraConst);
            if(palavrasAcertadas[nPalavra]!= 1) {
                palavrasNovas++;
                if (palavrasNovas != 1)
                    extensoPalavrasString += " - ";
                extensoPalavrasString += palavra;
                palavrasAcertadas[nPalavra] = 1;
            }
        }else{
            if(!verificar.equals("")) {
                fU.salvarExternalStorage(palavra + " -> " + verificar, getFilesDir());
                Log.i("Armazenamento", "Entrei para fazer o armazenamento");
            }
        }
         return;
//        else {
//            Log.d("Status", "Não acertou");
//            limparSeqEditText(palavraConst);
//        }
    }

    private void desabilitarSeqEditText(int[] palavraConst){
        for(int i= 0; i<palavraConst.length; i++){
            ((EditText)findViewById(palavraConst[i])).setEnabled(false);
        }
    }

    private void limparSeqEditText(int[] palavraConst){

        for(int i= 0; i<palavraConst.length; i++){
            EditText edtSeq = (EditText)findViewById(palavraConst[i]);

            if(edtSeq.isEnabled()) {
                edtSeq.setText("");
            }
        }
    }

    @Override
    public void onClick(View view) {
        verificarTudo();
    }
}
